
Public Module modMain

    Public objMainForm As New frmMain

    Public Sub main()

        If (CheckPrevInstance()) Then
            MessageBox.Show("TBV - TerraBit View has already running", "VMS", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        Application.EnableVisualStyles()
        Application.Run(objMainForm)

    End Sub



    Private Function CheckPrevInstance() As Boolean
        If UBound(Diagnostics.Process.GetProcessesByName _
         (Diagnostics.Process.GetCurrentProcess.ProcessName)) _
         > 0 Then
            Return True
        Else
            Return False
        End If
    End Function


    Public Sub showMDIChild(ByRef _mdiParent As System.Windows.Forms.Form, _
                     ByRef _mdiChild As System.Windows.Forms.Form, _
                       Optional ByVal newInstance As Boolean = False, _
                       Optional ByVal topMostInstance As Boolean = False)

        Dim frm As System.Windows.Forms.Form
        Dim bHasShowed As Boolean = False

        'Try
        If newInstance Then
            '   new instance
            If _mdiParent.IsMdiContainer Then
                _mdiChild.MdiParent = _mdiParent
            Else
                _mdiChild.MdiParent = objMainForm
            End If

            _mdiChild.TopMost = topMostInstance
            _mdiChild.StartPosition = FormStartPosition.CenterScreen
            _mdiChild.Show()
        Else
            '   re-use existing instance
            For Each frm In _mdiParent.MdiChildren
                If frm.Name.Trim.ToLower = _mdiChild.Name.Trim.ToLower Then
                    bHasShowed = True
                    Exit For
                End If
            Next

            If Not bHasShowed Then
                '   Set the proper MDI container
                If _mdiParent.IsMdiContainer Then
                    _mdiChild.MdiParent = _mdiParent
                Else
                    _mdiChild.MdiParent = objMainForm
                End If

                _mdiChild.StartPosition = FormStartPosition.CenterScreen
                _mdiChild.BringToFront()
                _mdiChild.Show()
            Else
                _mdiChild.Dispose()
                frm.TopMost = topMostInstance
                frm.Activate()
                frm.BringToFront()
            End If
        End If
        'Catch ex As Exception
        '    MessageBox.Show(ex.TargetSite.GetCurrentMethod.Name & ": " & _
        '        ControlChars.NewLine & ControlChars.Tab & _
        '        ex.Message, _mdiParent.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        'End Try
    End Sub



End Module
